package com.kasax.plugin.shadow.visitor

import com.kasax.plugin.shadow.utils.LogUtils
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.commons.ClassRemapper
import org.objectweb.asm.commons.Remapper

class PackageRemappingClassVisitor extends ClassRemapper {
    private static final String TAG = "PackageRemappingClassVisitor"
    private static final String OLD_PACKAGE = "com/kasax/testsdk/utils/"
    private static final String NEW_PACKAGE = "com/kasax/testsdk/utils/v2/"

    private static final String GSON_OLD_CLASS = "com/google/gson/Gson"

    private static final String GSON_NEW_CLASS = "com/google/gson/v2/Gson"


    PackageRemappingClassVisitor(ClassVisitor classVisitor) {
        super(classVisitor, new Remapper() {
            @Override
            String map(String internalName) {
                if (internalName.startsWith(OLD_PACKAGE)) {
                    def newName = NEW_PACKAGE + internalName.substring(OLD_PACKAGE.length())
                    LogUtils.d(TAG, "Remapping package: $internalName -> $newName")
                    return newName
                }
                if (internalName.equals(GSON_OLD_CLASS)) {
                    LogUtils.d(TAG, "Remapping class: $internalName -> $GSON_NEW_CLASS")
                    return GSON_NEW_CLASS
                }
                return internalName
            }
        })
    }
}
